package com.ruoyi.amuse.announcements.service.impl;


import com.ruoyi.amuse.announcements.domain.ClubAnnouncements;
import com.ruoyi.amuse.announcements.mapper.ClubAnnouncementsMapper;
import com.ruoyi.amuse.announcements.service.IClubAnnouncementsService;
import com.ruoyi.amuse.members.domain.ClubMembers;
import com.ruoyi.amuse.members.mapper.ClubMembersMapper;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * 发布公告Service业务层处理
 * 
 * @author juwujie
 * @date 2024-07-19
 */
@Service
public class ClubAnnouncementsServiceImpl implements IClubAnnouncementsService
{
    @Autowired
    private ClubAnnouncementsMapper clubAnnouncementsMapper;

    @Autowired
    private ClubMembersMapper clubMembers;

    /**
     * 查询发布公告
     * 
     * @param announcementId 发布公告主键
     * @return 发布公告
     */
    @Override
    public ClubAnnouncements selectClubAnnouncementsByAnnouncementId(Long announcementId)
    {
        return clubAnnouncementsMapper.selectClubAnnouncementsByAnnouncementId(announcementId);
    }

    /**
     * 查询发布公告列表
     * 
     * @param clubAnnouncements 发布公告
     * @return 发布公告
     */
    @Override
    public List<ClubAnnouncements> selectClubAnnouncementsList(ClubAnnouncements clubAnnouncements)
    {
//        将从数据库中查询到的图片地址转换为图片显示在前端
        /*
         * 根据登录的账号对成员表中的成员进行进行角色验证，各个社团的社长只能查看各自社团的信息
         * */
//        获取ruoyi当前要登录的账号
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userAccount = loginUser.getSysUser().getUserAccount();
        List<ClubMembers> clubMembers1 = clubMembers.selectClubMembersList(new ClubMembers(null, null, userAccount, null, null));
        for (ClubMembers clubMembers2 : clubMembers1){
            if (clubMembers2.getRoleId() == null){
                continue;
            }
            if (clubMembers2.getRoleId() == 1){
                clubAnnouncements.setClubId(clubMembers2.getClubId());
                //        将currentMember字段更新为clubMembers中当前成员人

                return clubAnnouncementsMapper.selectClubAnnouncementsList(clubAnnouncements);
            }else {
                continue;
            }
        }
        return clubAnnouncementsMapper.selectClubAnnouncementsList(clubAnnouncements);
    }

    /**
     * 新增发布公告
     * 
     * @param clubAnnouncements 发布公告
     * @return 结果
     */
    @Override
    public int insertClubAnnouncements(ClubAnnouncements clubAnnouncements)
    {
        // 获取当前时间
        LocalDateTime localDateTime = LocalDateTime.now();

        // 将 LocalDateTime 转换为 ZonedDateTime
        java.time.ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.systemDefault());

        // 从 ZonedDateTime 提取 Instant
        java.time.Instant instant = zonedDateTime.toInstant();

        // 将 Instant 转换为 Date
        Date date = Date.from(instant);

//        announcement_id 设置为当前时间的后6位加club_id
        clubAnnouncements.setAnnouncementId(System.currentTimeMillis() % 1000000 + clubAnnouncements.getClubId());
//        设置publishDate为当前时间
        clubAnnouncements.setPublishDate(date);

        /*
         * 根据登录的账号对成员表中的成员进行进行角色验证，各个社团的社长只能查看各自社团的信息
         * */
//        获取ruoyi当前要登录的账号
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userAccount = loginUser.getSysUser().getUserAccount();
        List<ClubMembers> clubMembers1 = clubMembers.selectClubMembersList(new ClubMembers(null, null, userAccount, null, null));
        for (ClubMembers clubMembers2 : clubMembers1){
            if (clubMembers2.getRoleId() == null){
                continue;
            }
            if (clubMembers2.getRoleId() == 1 && clubMembers2.getClubId().equals(clubAnnouncements.getClubId())){
                clubAnnouncements.setClubId(clubMembers2.getClubId());
                //        将currentMember字段更新为clubMembers中当前成员人

                return clubAnnouncementsMapper.insertClubAnnouncements(clubAnnouncements);
            }else {
                continue;
            }
        }

        return clubAnnouncementsMapper.insertClubAnnouncements(clubAnnouncements);
    }

    /**
     * 修改发布公告
     * 
     * @param clubAnnouncements 发布公告
     * @return 结果
     */
    @Override
    public int updateClubAnnouncements(ClubAnnouncements clubAnnouncements)
    {
        return clubAnnouncementsMapper.updateClubAnnouncements(clubAnnouncements);
    }

    /**
     * 批量删除发布公告
     * 
     * @param announcementIds 需要删除的发布公告主键
     * @return 结果
     */
    @Override
    public int deleteClubAnnouncementsByAnnouncementIds(Long[] announcementIds)
    {
        return clubAnnouncementsMapper.deleteClubAnnouncementsByAnnouncementIds(announcementIds);
    }

    /**
     * 删除发布公告信息
     * 
     * @param announcementId 发布公告主键
     * @return 结果
     */
    @Override
    public int deleteClubAnnouncementsByAnnouncementId(Long announcementId)
    {
        return clubAnnouncementsMapper.deleteClubAnnouncementsByAnnouncementId(announcementId);
    }
}
